package com.gd.coreservice.controller;


import com.gd.commonutils.Constants;
import com.gd.commonutils.Result;
import com.gd.coreservice.entity.Orders;
import com.gd.coreservice.entity.vo.OrdersVo;
import com.gd.coreservice.service.OrdersService;
import com.gd.servicebase.exceptionhandler.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2024-03-23
 */
@RestController
@RequestMapping("/orders")
@CrossOrigin
public class OrdersController {
    @Autowired
    private OrdersService orderService;
    //查询所有的订单  已测试
    @GetMapping("/getOrder")
    public List<Orders> getOrder() {
        return orderService.findAll();
    }
    //根据订单ID查询  已测试
    @GetMapping("/getOneOrder")
    public Orders getOneOrder(@RequestParam("Oid") String Oid) {
        return orderService.getOneOrder(Oid);
    }
    //根据买家id查询数据  这里的模糊查询oid有点问题  因为是模糊查询 所有基本无论输入任何数字都能匹配成功  已测试
    @GetMapping("/getOneOrderBySeller")
    public Map<String, Object> findOneBUY(@RequestParam("SellerUid") String SellerUid, @RequestParam("Oid") String Oid){
        List<OrdersVo> data = orderService.findOneBUY(SellerUid,Oid);
        Integer total = data.size();
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }

    //添加订单  已测试
    @PostMapping("/insertOrder")
    public Result addOrder(@RequestParam("BUYSid") String BUYSid, @RequestParam("SELLSid") String SELLSid,
                           @RequestParam("Gid") String Gid, @RequestParam("Oprice") String Oprice) {
        if(orderService.insertOrder(BUYSid,SELLSid,Gid,Oprice))
        {
            return Result.success("交易成功");
        }
        else
        {
            throw new ServiceException(Constants.CODE_600, "后台异常");//自定义异常
        }

    }
    //删除  已测试
    @DeleteMapping("/deleteOrder/{Oid}")
    public Integer deleteOrder(@PathVariable("Oid") String Oid){
        return orderService.deleteOrder(Oid);
    }
    //批量删除  已测试
    @PostMapping("/deleteOrder/batch")
    public Integer deleteOrderBatch(@RequestBody List<String> Oids){
        return orderService.deleteOrderBatch(Oids);
    }

    //分页查询  已测试
    @GetMapping("/getOrderPage")
    public Map<String, Object> getOrderPage(@RequestParam Integer pageNum,
                                            @RequestParam Integer pageSize,
                                            @RequestParam String Oid) {
        Map<String,Object> result = orderService.getOrderPage(pageNum, pageSize, Oid);
        List<Orders> data =(List<Orders>)result.get("List");
        Long total =(Long) result.get("total");
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }

    //查询总订单数 已测试
    @GetMapping("/getOrderAllTotal")
    public int getAllTotal(){
        return orderService.getAllTotal();
    }

    //查询所有订单的总成交价格  已测试
    @GetMapping("/getOrderPriceTotal")
    public String getPriceTotal(){
        return orderService.getPriceTotal();
    }

}

